package com.cloudsigns.factorymanager2.engineering.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.LoginActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.activity.AddEngineeringFeedBackActivity;
import com.cloudsigns.factorymanager2.engineering.activity.AddEngineeringFeedBackActivity1;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringInstallDailyFeedBackDetailActivity;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringInstallFeedBackReplyForTeamActivity;
import com.cloudsigns.factorymanager2.engineering.adapter.DailyFeedBackForEngineeringInstallAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 日反馈
 * Created by tony on 2018/4/6.
 */

public class DailyFeedBackForEngineeringInstallFragment extends Fragment implements View.OnClickListener, ItemClickListener {
    private View rl_abnormal_return;
    private ListView lv_abnormal_return;
    private ScrollView scrollview;
    private LinearLayout ll_feedback;
    private ImageView img_abnormal_return;
   private List<String> dayStrList;
    private HashMap<String, List<EngineeringDailyFeedBackBean>> feedBackMap;
    private LayoutInflater layoutInflater;
    private DailyFeedBackForEngineeringInstallAdapter replayAdapter;
    private List<EngineeringDailyFeedBackBean> repalyDataList = new ArrayList<>();
    private EngineeringInstallationOrderBean engineeringInstallationOrderBean;
    private String TAG = DailyFeedBackForEngineeringInstallFragment.class.getSimpleName();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_engineeringinstall_dailyfeedback_for_super, null);
        rl_abnormal_return = view.findViewById(R.id.rl_abnormal_return);
        lv_abnormal_return = view.findViewById(R.id.lv_abnormal_return);
        img_abnormal_return = view.findViewById(R.id.img_abnormal_return);
        ll_feedback = view.findViewById(R.id.ll_feedback);
        scrollview = view.findViewById(R.id.scrollview);

        layoutInflater = inflater;

        img_abnormal_return.setBackgroundResource(R.mipmap.arrow_down_icon);

        rl_abnormal_return.setOnClickListener(this);
        img_abnormal_return.setOnClickListener(this);

        replayAdapter = new DailyFeedBackForEngineeringInstallAdapter(getActivity(), TAG);
        replayAdapter.setData(repalyDataList);
        replayAdapter.setItemClick(new ItemClickListener() {
            @Override
            public void itemClick(View view, int position) {
                EngineeringDailyFeedBackBean engineeringDailyFeedBackBean = repalyDataList.get(position);
                Intent mIntent = new Intent(getActivity(), EngineeringInstallFeedBackReplyForTeamActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, engineeringDailyFeedBackBean);
                mIntent.putExtra(ConstantUtil.KEY_2, engineeringInstallationOrderBean);
                startActivity(mIntent);
            }
        });
        lv_abnormal_return.setAdapter(replayAdapter);
        return view;
    }

    private void updateUi() {
        if (null != dayStrList && null != ll_feedback) {
            ll_feedback.removeAllViews();

            for (String date : dayStrList) {
                View view = layoutInflater.inflate(R.layout.layout_daily_feedback_for_engineeringinstall, null);
                TextView tv_date = view.findViewById(R.id.tv_date);
                TextView tv_week = view.findViewById(R.id.tv_week);
                ImageView img_add_feedback = view.findViewById(R.id.img_add_feedback);
                ListView lv_feedback = view.findViewById(R.id.lv_feedback);
                tv_date.setText(date);
                String week = DateUtil.getWeekByDateStr(date);
                tv_week.setText(week);
                img_add_feedback.setOnClickListener(this);

                long currentMill = System.currentTimeMillis();
                String currentDate = DateUtil.getYyyy_Mm_Dd(currentMill);
                if (date.equals(currentDate)) {
                    img_add_feedback.setVisibility(View.VISIBLE);
                } else {
                    img_add_feedback.setVisibility(View.GONE);
                }

                final List<EngineeringDailyFeedBackBean> dataList = new ArrayList<>();

                if (feedBackMap.containsKey(date)) {
                    dataList.addAll(feedBackMap.get(date));
                } else {
                    EngineeringDailyFeedBackBean engineeringDailyFeedBackBean = new EngineeringDailyFeedBackBean();
                    engineeringDailyFeedBackBean.setNoFeedBack(true);
                    dataList.add(engineeringDailyFeedBackBean);
                }
                DailyFeedBackForEngineeringInstallAdapter adapter = new DailyFeedBackForEngineeringInstallAdapter(getActivity(), TAG);
                adapter.setData(dataList);
                adapter.setItemClick(this);
                lv_feedback.setAdapter(adapter);


                ll_feedback.addView(view);


            }
        }

        scrollview.smoothScrollTo(0, 0);

    }

    public void initData(EngineeringInstallationOrderBean orderBean) {
        if (null != orderBean) {
            feedBackMap = new HashMap<>();
            List<EngineeringDailyFeedBackBean> allEngineeringDailyFeedbackList = orderBean.getEngineeringDailyFeedbackList();
            if (null != allEngineeringDailyFeedbackList) {
                repalyDataList.clear();
                feedBackMap.clear();

                for (EngineeringDailyFeedBackBean engineeringDailyFeedBackBean : allEngineeringDailyFeedbackList) {
//                    String accountId = engineeringDailyFeedBackBean.getAccountId();
//                    String handlerAccountId = engineeringDailyFeedBackBean.getHandlerAccountId();
//                    String completeUnusualAccountId = engineeringDailyFeedBackBean.getCompleteUnusualAccountId();
//                    String confirmCompleteUnusualAccountId = engineeringDailyFeedBackBean.getConfirmCompleteUnusualAccountId();

                    CommonEnum.UnusualStatus unusualStatus = engineeringDailyFeedBackBean.getUnusualStatus();
                    if (null != unusualStatus) {
                        if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY || unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY_CONFIRM || unusualStatus == CommonEnum.UnusualStatus.REPLY_CONFIRM_FINISH)
                            repalyDataList.add(engineeringDailyFeedBackBean);

                    }

                    String key = DateUtil.getYyyy_Mm_Dd(engineeringDailyFeedBackBean.getFeedbackDate());
                    if (feedBackMap.containsKey(key)) {
                        feedBackMap.get(key).add(engineeringDailyFeedBackBean);
                    } else {
                        List<EngineeringDailyFeedBackBean> engineeringDailyFeedBackBeanList = new ArrayList<>();
                        engineeringDailyFeedBackBeanList.add(engineeringDailyFeedBackBean);
                        feedBackMap.put(key, engineeringDailyFeedBackBeanList);
                    }

                }

                replayAdapter.setData(repalyDataList);
                replayAdapter.notifyDataSetChanged();
            }
            engineeringInstallationOrderBean = orderBean;
//
            long receiveOrderTime = engineeringInstallationOrderBean.getReceiveOrderTime();
            long currentTime = System.currentTimeMillis();


            Date beginDate = new Date(receiveOrderTime);
            Date currentDate = new Date(currentTime);

            int days = DateUtil.differentDays(beginDate,currentDate);
//            int days = DateUtil.getBetweenDays(receiveOrderTime, currentDate);
            dayStrList = new ArrayList<>();
            if (days >= 0) {

                for (int i = days; i >= 0; i--) {
                    long dayTime = i * 24 * 60 * 60 * 1000L;
                    long nextDate = receiveOrderTime + dayTime;
                    dayStrList.add(DateUtil.getYyyy_Mm_Dd(nextDate));
                }

            }
//            }

            updateUi();
        }
    }

    // 异常回复 收放
    private void showEngineeringInfoDialog() {
        if (lv_abnormal_return.getVisibility() == View.VISIBLE) {
            lv_abnormal_return.setVisibility(View.GONE);
            img_abnormal_return.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            lv_abnormal_return.setVisibility(View.VISIBLE);
            img_abnormal_return.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.img_add_feedback:
                if (null != engineeringInstallationOrderBean) {
                    mIntent = new Intent(getActivity(), AddEngineeringFeedBackActivity.class);
//                    mIntent = new Intent(getActivity(), AddEngineeringFeedBackActivity1.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, engineeringInstallationOrderBean);
                    startActivity(mIntent);

                }

                break;
            // 异常回复 收放
            case R.id.img_abnormal_return:
                showEngineeringInfoDialog();
                break;
        }
    }

    @Override
    public void itemClick(View view, int position) {
        EngineeringDailyFeedBackBean engineeringDailyFeedBackBean = (EngineeringDailyFeedBackBean) view.getTag();
        if (null != engineeringDailyFeedBackBean) {
            if (engineeringDailyFeedBackBean.isNoFeedBack()) {

            } else {
                Intent mIntent = new Intent(getActivity(), EngineeringInstallDailyFeedBackDetailActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, engineeringDailyFeedBackBean);
                startActivity(mIntent);
            }
        }
    }

}
